<?php
declare (strict_types=1);

namespace app\course\logic\comment\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\course\logic\base\admin\Course;
use app\course\model\comment\admin\Comment as CommentModel;
use app\user\logic\user\admin\Account;

/**
 * Class Comment
 *
 * @package app\course\logic\comment\admin
 */
class Comment extends CommentModel
{
    /**
     * 评论列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/21
     *
     * @param array  $map
     * @param string $phone
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $map = [], string $phone = '')
    {
        $builder = $this->with(['user']);
        if ($phone) {
            // $user_id = Account::withoutGlobalScope(['mhm'])->where('phone', $phone)->removeOption('soft_delete')->value('id');
            $user_id = Account::where('phone', $phone)->removeOption('soft_delete')->value('id');

            $builder = $builder->where('user_id', $user_id);
        }

        return $builder->where($map)->order('create_time', 'DESC')->paginate($this->getListRows());
    }

    /**
     * 删除评论
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/22
     *
     * @param int $courseId
     * @param int $commentId
     */
    public function deleteComment(int $courseId, int $commentId)
    {
        $advert = $this->where('id', $commentId)->findOrFail();

        if (!$advert->delete()) {
            throw new LogicException("删除失败");
        }

        $title = Course::where('id', $courseId)->value('title');
        Log::write('删除课程【' . $title . '】评论内容【' . $advert->content . '】', 'course_comment', 'delete', true);
    }

}